<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use manniu\controller\AdminBase;

use app\admin\model\Site as SiteModel;

class Site extends AdminBase{

    //当前控制器菜单
    public $submenu = [];
	/**
	 * TP初始化方法
	 */
	public function _initialize(){
		parent::_initialize();
        $this->submenu[] = ['title' => '网站配置', 'url' => url('admin/system/index'), 'icon' => '&#xe614;'];
	}

    /**
     * 网站配置
     * @access public
     * @return \think\Response
     */
    public function index(){
        $website = SiteModel::get(1);
    	if($this->request->isPost()){
    		//所有配置项标签
    		$_data = $this->request->param('data/a');
    		$tc_setting = [];
    		$config_tag = ['site_status','site_close_msg','site_name','site_time_format','site_language','site_theme','site_template','site_timezone','site_domains','site_rewrite','site_mobile_open','site_mobile','site_seojoin','site_title','site_keywords','site_description','site_image_ratio','site_image_watermark','site_image_remote','site_image_vrtalign','site_image_horalign','site_image_vrtoffset','site_image_horoffset','site_image_type','site_image_overlay','site_image_opacity','site_image_font','site_image_color','site_image_size','site_image_text','site_domain','site_navigator','site_image_content','site_attach_url','site_attach_host','site_mobile_template'];

    		//开关配置项标签
    		$switch_config_tag = ['site_status','site_rewrite','site_mobile_open','site_image_ratio','site_image_watermark','site_image_remote','site_image_content'];

    		//数字配置项标签
    		$int_config_tag = ['site_image_type','site_image_vrtoffset','site_image_horoffset','site_image_opacity','site_image_size'];

            //获取配置项
    		foreach ($_data as $key => $value) {
                if((in_array($key, $switch_config_tag) || in_array($key, $int_config_tag)) && in_array($key, $config_tag)){ //获取开关与数字配置项
                    if(($value == 'TRUE' || $value == 'true') && strlen($value) == 4){
                        $tc_setting[$key] = 1;
                    }else{
                        $tc_setting[$key] = (int)$value;
                    }
                }elseif(in_array($key, $config_tag)){ //获取其它配置项
                    $tc_setting[$key] = $value;
                }
    		}

            //修补配置项
    		foreach ($config_tag as $c_tag) {
                //修补未提交的开关配置项
                if(!isset($tc_setting[$c_tag]) && in_array($c_tag, $switch_config_tag)){
                    $tc_setting[$c_tag] = 0;
                }
                //修补未提交的数字配置项
                if(!isset($tc_setting[$c_tag]) && in_array($c_tag, $int_config_tag)){
                    $tc_setting[$c_tag] = 0;
                }
                //修补未提交的其它配置项
                if(!isset($tc_setting[$c_tag]) && in_array($c_tag, $config_tag)){
                    $tc_setting[$c_tag] = '';
                }
            }
            if(false !== $website->save(['setting'=>$tc_setting],['id'=>1])){
                $this->success('操作成功','index');
            }else{
                $this->error('修改失败，请稍后重试');
            }
    	}
    	$this->assign([
            'submenu' => $this->submenu,
            'data' => $website->setting,
        ]);
    	return $this->fetch();
    }
}